#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.basic.module.group.service.imp;

import ${package}.basic.module.group.mapper.GroupMapper;
import ${package}.basic.module.group.service.GroupService;
import ${package}.common.bean.po.Group;
import ${package}.common.bean.po.Tree;
import ${package}.common.bean.vo.QueryBean;
import ${package}.common.emum.GroupEnum;
import ${package}.common.emum.ReturnCodeEnum;
import ${package}.common.exception.ValidException;
import ${package}.common.utils.TreeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 无法补充
 *
 * @author YL
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Slf4j
@Service
@AllArgsConstructor
public class GroupServiceImpl implements GroupService {

    /**
     * 组织Service
     */
    private GroupMapper groupMapper;

    /**
     * Description:
     * <根据id查找机构>
     * @author 周婷婷
     * @date 17:05 2019/8/6
     * @param id 1
     * @return Group
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Group getGroupById(Long id) {
        return groupMapper.getGroupById(id);
    }

    /**
     * Description:
     * <根据id查找该机构到根节点的所有机构>
     * @author 周婷婷
     * @date 17:05 2019/8/6
     * @param id 1
     * @return java.util.List<Group>
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Group> getGroupChain(Long id){
        List<Group> list = new ArrayList<>();
        Group group = groupMapper.getGroupById(id);
        while(group != null && group.getId().longValue() != group.getParentId().longValue()){
            list.add(group);
            group = groupMapper.getGroupById(group.getParentId());
        }
        return list;
    }

    /**
     * Description:
     * <按条件分页获取机构>
     * @author 周婷婷
     * @date 17:06 2019/8/6
     * @param queryBean 1
     * @return com.github.pagehelper.PageInfo<Group>
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageInfo<Group> queryGroupListByLimit(QueryBean queryBean){

        Page<Group> page = PageMethod.startPage(queryBean.getCurrent(), queryBean.getPageSize());
        groupMapper.queryGroupListByLimit(queryBean);

        return new PageInfo<>(page);
    }

    /**
     * Description:
     * <获取机构类型列表>
     * @author 周婷婷
     * @date 17:06 2019/8/6
     * @return java.util.List<java.lang.Integer>
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Integer> getGroupTypeList(){
        return groupMapper.getGroupTypeList();
    }
    /**
     * Description:
     * <新增机构>
     * @author 周婷婷
     * @date 17:06 2019/8/6
     * @param group 1
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addGroup(Group group){
        groupMapper.insertGroup(group);
    }


    /**
     * Description:
     * <查询机构编码是否存在>
     * @author 周婷婷
     * @date 17:07 2019/8/6
     * @param code 1
     * @param id 2
     * @return java.lang.Boolean
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean groupVerifyCode(String code,Long id){
        if(id == null){
	        return CollectionUtils.isEmpty(groupMapper.findGroupByCode(code));
        } else {
	        return CollectionUtils.isEmpty(groupMapper.findGroupByCodeOrNotInId(code, id));
        }
    }

    /**
     * Description:
     * <根据id更新机构信息>
     * @author 周婷婷
     * @date 17:07 2019/8/6
     * @param group 1
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateGroupById(Group group){

        // 叶子节点：部门，无子机构的公司
        // 非叶子节点不能修改机构类型和上级机构，叶子节点无限制
        Group groupBefore = groupMapper.getGroupById(group.getId());

        // 修改后的机构与修改前机构对比，若修改了类型和上级机构，判断是否是叶子节点
        boolean isLeaf = (groupBefore.getType().intValue() != group.getType().intValue() ||
                groupBefore.getParentId().longValue() != group.getParentId().longValue()) && groupBefore.getType().intValue() != GroupEnum.DEPARTMAENT.getCode();
        if(isLeaf) {
            List<Group> list = groupMapper.findChildGroup(groupBefore.getId());
            if (CollectionUtils.isNotEmpty(list)) {
                throw new ValidException(ReturnCodeEnum.CURRENT_ELEMENT_WAS_OCCUPIED);
            }
        }

        groupMapper.updateGroupById(group);
    }

    /**
     * Description:
     * <根据id删除机构>
     * @author 周婷婷
     * @date 17:07 2019/8/6
     * @param id 1
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteGroup(Long id) {
        // 若是部门或无子机构的公司，即可删除
        Group group = groupMapper.getGroupById(id);
        if(group.getType().intValue() != GroupEnum.DEPARTMAENT.getCode()) {
            List<Group> list = groupMapper.findChildGroup(id);
            if (CollectionUtils.isNotEmpty(list)) {
                throw new ValidException(ReturnCodeEnum.CURRENT_ELEMENT_WAS_OCCUPIED);
            }
        }
        groupMapper.deleteGroup(id);
    }

    /**
     * Description:
     * <获取组织树>
     * @author songcx
     * @date 10:45 2019/9/4
     * @return java.util.List<${package}.common.bean.po.Tree>
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Tree> getGroupTree(){
        List<Tree> groupTrees = groupMapper.getGroupTree();
        return TreeUtil.fillingTree(groupTrees);
    }

}
